<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('供电所名单新增')"/>
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">任务年月</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="evaluationDate" name="evaluationDate" id="evaluationDate"
                               placeholder="请选择评价任务年月"
                               type="text">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-filter="role-save" lay-submit="" type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="layui-btn layui-btn-primary layui-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#evaluationDate' //指定元素
            ,type: 'month'
        })


        form.verify({
            evaluationDate: function (value) {
                console.log(value)
                if (value.length < 1) {
                    return '评价任务年月不能为空';
                }
                var msg = "";
                $.ajax({
                    url: rootPath + '/MonthEvaluationController/checkMonthUnique',
                    data: "evaluationDate=" + value,
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (result) {
                        msg = result;
                    }
                });
                if (msg == 1) {
                    return "当前年月已存在月评价任务，不允许重复创建";
                }
            }
        });


        form.on('submit(role-save)', function (data) {
            var loading = layer.load(2, {
                shade: [0.6, '#000'], //0.4为透明度 ，#000 为颜色
                content: '月度评价任务正在生成中，请耐心等待，不要关闭此弹窗...',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '45px',
                        'width': '100px',
                        'background-position': 'top center',
                        'color': '#fff',
                        'margin-left': '0px',
                        'text-align': 'center',
                    });
                }
            });
            console.log(data.field)
            $.ajax({
                url: rootPath + '/MonthEvaluationController/add',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                // beforeSend: function(){
                //     index_wait = layer.load(0, {  //发送请求前调用load方法
                //         shade: [0.5, '#fff'],  //0.5透明度的白色背景
                //         content: '评价任务正在生成，请耐心等待...',
                //     });
                // },
                // complete: function () {  //load默认不会关闭，请求完成需要在complete回调中关闭
                //     layer.close(index_wait);
                // },
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("monthEvaluation-table");
                        });
                        loading.close();
                    } else {
                        loading.close();
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>